package com.example.client02.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@RabbitListener(queues = "sms")
@Slf4j
@Component
public class SmsListener {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitHandler
    public void execute(String str){
        //redis有没有这个订单号
        if(redisTemplate.hasKey(str)){
            log.error("{}出现了重复",str);
            return;
        }else{
            //处理业务
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            log.info("{}号业务正在被处理");
            redisTemplate.opsForValue().set(str,1);
        }



    }
}
